package com.lp.model.utils;

import com.lp.model.config.AppResult;
import com.lp.model.config.CodeMsg;
import com.lp.model.dto.article.PageDto;
import com.lp.model.dto.search.SearchPageDto;
import com.lp.model.entity.article.Article;
import com.lp.model.entity.article.Collects;
import com.lp.model.entity.article.Comments;
import com.lp.model.entity.article.vo.ArticleVO;
import com.lp.model.entity.article.vo.PageVO;
import com.lp.model.exception.ApplicationException;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;

@Slf4j
public class PageUtils {

    /**
     * 校验分页信息是否合法
     * @param page 分页信息 + id
     */
    public static boolean checkPageDto(PageDto page) {
        if(page == null || page.getPageSize() == null || page.getCurPage() == null
                || page.getCurPage() <= 0 || page.getPageSize() <= 0) {
            return false;
        }
        if(page.getId() != null && page.getId() <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 封装分页响应数据
     * @param list 分页列表
     * @param total 总数
     * @param <T> 评论、文章......
     */
    public static <T> PageVO<T> getPageVO(List<T> list, Long total) {
        PageVO<T> pageVO = new PageVO<>();
        pageVO.setList(list);
        pageVO.setTotal(total);
        return pageVO;
    }

    /**
     * 检查搜索分页数据异常情况
     * @param page 分页信息 + 搜索内容
     * @return
     */
    public static boolean checkSearchPageDto(SearchPageDto page) {
        if(page == null || page.getPageSize() == null || page.getCurPage() == null
                || page.getCurPage() <= 0 || page.getPageSize() <= 0) {
            return false;
        }
        return true;
    }

}
